package com.hnou.boxuegu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hnou.boxuegu.R;
import com.hnou.boxuegu.bean.ExercisesDetailBean;
import com.hnou.boxuegu.utils.UtilsHelper;

import java.util.ArrayList;
import java.util.List;

public class ExercisesDetailAdapter extends BaseAdapter {

    private Context mContext;
    private List<ExercisesDetailBean> edbl;
    private ArrayList<String> SelectedPosition = new ArrayList<>();
    private OnSelectListener onSelectListener;

    public ExercisesDetailAdapter(Context context,OnSelectListener onSelectListener){
        this.mContext=context;
        this.onSelectListener = onSelectListener;
    }
    public void setData(List<ExercisesDetailBean> edbl){
        this.edbl=edbl;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return edbl == null ? 0 : edbl.size();
    }

    @Override
    public ExercisesDetailBean getItem(int position) {
        return edbl == null ? null :edbl.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private ArrayList<String> selectedPosition = new ArrayList<String>();
    @Override
    public View getView(final int position, View concertView, ViewGroup parent) {
        final ViewHolder vh;
        if(concertView == null){
            vh=new ViewHolder();
            concertView = LayoutInflater.from(mContext).inflate(R.layout.exercises_detail_list_item,null);
            vh.subject=concertView.findViewById(R.id.tv_subject);
            vh.tv_a=concertView.findViewById(R.id.tv_a);
            vh.tv_b=concertView.findViewById(R.id.tv_b);
            vh.tv_c=concertView.findViewById(R.id.tv_c);
            vh.tv_d=concertView.findViewById(R.id.tv_d);
            vh.iv_a=concertView.findViewById(R.id.iv_a);
            vh.iv_b=concertView.findViewById(R.id.iv_b);
            vh.iv_c=concertView.findViewById(R.id.iv_c);
            vh.iv_d=concertView.findViewById(R.id.iv_d);
            concertView.setTag(vh);
        }
        else {
            vh=(ViewHolder)concertView.getTag();
        }
        final ExercisesDetailBean bean = getItem(position);
        if(bean != null){
            vh.subject.setText(bean.getSubject());
            vh.tv_a.setText(bean.getA());
            vh.tv_b.setText(bean.getB());
            vh.tv_c.setText(bean.getC());
            vh.tv_d.setText(bean.getD());
        }if(!selectedPosition.contains(""+position)){
            vh.iv_a.setImageResource(R.drawable.exercises_a);
            vh.iv_b.setImageResource(R.drawable.exercises_b);
            vh.iv_c.setImageResource(R.drawable.exercises_c);
            vh.iv_d.setImageResource(R.drawable.exercises_d);
            UtilsHelper.setABCDEnable(true,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
        }else {
            UtilsHelper.setABCDEnable(false,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            switch (bean.getSelect()){
                case 0:
                    if(bean.getAnswer() == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 2){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 3) {
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 4){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 1:
                    vh.iv_a.setImageResource(R.drawable.exercises_error_icon);
                    if(bean.getAnswer() == 2){
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 3){
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 4){
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 2:
                    vh.iv_b.setImageResource(R.drawable.exercises_error_icon);
                    if(bean.getAnswer() == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 3){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 4){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 3:
                    vh.iv_c.setImageResource(R.drawable.exercises_error_icon);
                    if(bean.getAnswer() == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else  if(bean.getAnswer() == 2){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_d.setImageResource(R.drawable.exercises_d);
                    }else if(bean.getAnswer() == 4){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_d.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 4:
                    vh.iv_d.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.getAnswer() == 1){
                        vh.iv_a.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                    }else if (bean.getAnswer() == 2){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_right_icon);
                        vh.iv_c.setImageResource(R.drawable.exercises_c);
                    }else if(bean.getAnswer() == 3){
                        vh.iv_a.setImageResource(R.drawable.exercises_a);
                        vh.iv_b.setImageResource(R.drawable.exercises_b);
                        vh.iv_c.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                default:
                    break;

            }

        }
        vh.iv_a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPosition(position);
                onSelectListener.onSelectA(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }

            private void selectedPosition(int position) {
            }
        });
        vh.iv_b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPosition(position);
                onSelectListener.onSelectB(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }

            private void selectedPosition(int position) {
            }
        });
        vh.iv_c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPosition(position);
                onSelectListener.onSelectC(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }

            private void selectedPosition(int position) {
            }
        });
        vh.iv_d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPosition(position);
                onSelectListener.onSelectD(position,vh.iv_a,vh.iv_b,vh.iv_c,vh.iv_d);
            }
        });

        return concertView;
    }
    private void selectedPosition(int position) {
        if(!selectedPosition.contains(""+position)){
            selectedPosition.add(position+"");
        }
    }
    class ViewHolder{
        public TextView subject,tv_a,tv_b,tv_c,tv_d;
        public ImageView iv_a,iv_b,iv_c,iv_d;
    }
    public interface OnSelectListener{
        void onSelectA(int position, ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);
        void onSelectB(int position, ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);
        void onSelectC(int position, ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);
        void onSelectD(int position, ImageView iv_a,ImageView iv_b,ImageView iv_c,ImageView iv_d);

    }
}
